package com.elem.dao.impl;

import com.elem.bean.OrderDetailet;
import com.elem.dao.OrderDetailetDao;
import com.elem.util.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Collections;
import java.util.List;

public class OrderDetailetDaoImpl implements OrderDetailetDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public int saveOrderDetailetBatch(List<OrderDetailet> list) throws Exception {
        String sql = "insert into orderdetailet(orderId,foodId,quantity) values(?,?,?)";
        for (OrderDetailet orderDetailet : list) {
            jdbcTemplate.update(sql,orderDetailet.getOrderId(),orderDetailet.getFoodId(),orderDetailet.getQuantity());
        }
        return 0;
    }

    @Override
    public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId) throws Exception {
        String sql = "select * from orderdetailet where orderId=?";
        List<OrderDetailet> orderDetailets = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(OrderDetailet.class), orderId);
        return orderDetailets;
    }
}
